package com.tanhua.server.service;

import com.alibaba.fastjson.JSON;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.PublishApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 发送动态评分消息，工具类
 */
@Component
public class MovementsMQService {

    @Reference
    private PublishApi publishApi;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 发送消息
     * type 1-发动态，2-浏览动态， 3-点赞， 4-喜欢， 5-评论，6-取消点赞，7-取消喜欢
     */
    public void sendMsg(String publishId, Integer type){
        // 定义消息内容
        Map<String, Object> map = new HashMap<>();
        map.put("type", type);
        map.put("userId", UserHolder.getUserId());
        map.put("publishId", publishId);
        Publish publish = publishApi.findById(publishId);
        map.put("pid", publish.getPid());

        // 发送消息
        rocketMQTemplate.convertAndSend("tanhua-quanzi", JSON.toJSONString(map));

    }
}
